package org.apache.ocean.main.search;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;

import org.apache.lucene.search.Query;
import org.apache.ocean.main.Index.IndexSnapshot;
import org.apache.ocean.main.search.Search.FacetQuery;
import org.apache.ocean.main.search.Search.FacetResult;
import org.apache.ocean.main.search.Search.FacetValue;
import org.apache.ocean.main.search.cache.QueryDocSetCacheKey;
import org.apache.ocean.main.search.cache.QueryFacetCacheKey;

public class IndexFacetQuerySearch implements Callable<FacetResult> {
  private List<FacetQuery> queries;
  private IndexSnapshot indexSnapshot;
  private QueryDocSetCacheKey queryDocSetCacheKey;
	
	public IndexFacetQuerySearch(List<FacetQuery> queries, QueryDocSetCacheKey queryDocSetCacheKey, IndexSnapshot indexSnapshot) {
		this.queries = queries;
		this.queryDocSetCacheKey = queryDocSetCacheKey;
		this.indexSnapshot = indexSnapshot;
	}

	public FacetResult call() throws Exception {
		List<FacetValue> facetValues = new LinkedList<FacetValue>();
		for (FacetQuery facetQuery : queries) {
			Query query = facetQuery.getQuery();
		  int count = indexSnapshot.getFacetValue(new QueryFacetCacheKey(query, queryDocSetCacheKey));
		  facetValues.add(new FacetValue(query.toString(), count));
		}
		return new FacetResult(facetValues);
	}
}
